//载入那些函数库
let includes = ([
    "include/common.js",
]);

let inherits = ([
    "inherit/data.js",
]);

function create() {
    return thisObject;
}


function main(me, arg) {


    if (arg == "aaa") {
        tellObject(me, "热更新成功！").to();
        return;
    }

    if (arg == "bbb") {
        tellObject(me, "热更新成功！").to();
        return;
    }


    /**
     * 先查找模板进行修改
     * 要区别是不是头文件
     */
    // let metaCls = findMetaClass(arg);
    //
    // if( !functionp(metaCls) && !objectp(metaCls) ){
    //     //模板类异常
    //     return;
    // }

    loadFile(me, arg);
}

function loadFile(me, file) {


    let gt = os.getGlobalThis();

    let idx = gt.metaClassSource.findIndex(e => {
        return e.file == file;
    });

    //复制必要信息
    let backAtt = {};
    if (!objectp(gt.metaClassSource[idx]) && !functionp(gt.metaClassSource[idx])) {
        tellObject(me, "内存中没有该文件，无需更新！").to();
        return;
    }


    //先寻找更新的对象
    let attrs = Object.keys(gt.metaClassSource[idx].cmpl);

    let needAttr = [
        "__metaType__",
        "__parentFunc__",
        "__parents__meta__file__",
        "__subsFile__",
    ]
    attrs.forEach(e => {
        if (needAttr.includes(e)) {
            backAtt[e] = gt.metaClassSource[idx].cmpl[e];
        }
    });

    gt.metaClassSource.splice(idx, 1);

    let core = require("include/core.js");

    //加载新的类模板...
    core.loadScriptToCmplObj(file);
    let newMetaClass = core.findMetaClass(file).cmpl;

    if (!objectp(newMetaClass) && !functionp(newMetaClass)) {
        tellObject(me, "类模板加载失败！").to();
        return;
    }

    needAttr.forEach(e => {
        newMetaClass[e] = backAtt[e];
    });

    //type 1 房间 2 其他
    let type, upOb = loadObject(file),bakExits;

    //
    // if (!objectp(environment(me))) {
    //     tellObject(me, "你所在的环境有问题!").to();
    // }

    if (upOb == environment(me)) {
        tellObject(me, "你销毁的对象是你的环境!").to();
        let voidRoom = loadObject("clone/misc/void.js");
        //如果是房间就备份房间的连接 内容物就用移动的方式
        if (upOb.get("obType") == "isRoom") {
            type = 1;
            bakExits = upOb.get("exits");
        }
        me.move(voidRoom);
    }

    // os.debug(fileNameSid(upOb), fileNameSid(environment(me)), "环境情况");


    //销毁OB并重新生成
    if (!destruct(upOb)) {
        tellObject(me, "更新时对象销毁失败!").to();
        return;
    }
    //生成新的对象
    upOb = loadObject(file);
    //如 之前销毁的是房间，恢复房间出口...
    if (type == 1) {

        //连接表
        let fillRouter = {
            east:"west",
            west:"east",
            north:"south",
            south:"north",
        }

        upOb.set("exits", bakExits);
        let keys = Object.keys(bakExits);
        // os.debug(keys,"debuger");
        keys.forEach(e=>{
                //e 是方向
            // os.debug(bakExits[e],"debuger");
                let rOb = findObject( bakExits[e] );
            // os.debug(rOb,"debuger");
                //找出连接房间的对应的反方向 就是我自己
            rOb.get("exits")[ fillRouter[e] ] = fileNameSid(upOb);

            // os.debug(rOb,fileNameSid(upOb),"debuger");
        })
        me.move(upOb);
    }


    tellObject(me, "更新完成....").to();
    // os.debug(newMetaClass,"newMetaClass","debuger");
    //
    // os.debug(backAtt, idx, gt.metaClassSource, gt.metaClassSource[idx].cmpl, gt.metaClassSource[idx].file, file, "backAtt", "debuger");


}